﻿using IOA.MES.Business;
using IOA.MES.DataAccess.VModel;
using IOA.MES.PdaApi.Models.Request.Oba;
using IOA.MES.PdaApi.Models.Response.Oba;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace IOA.MES.PdaApi.Controllers
{
    /// <summary>
    /// OBA接口
    /// </summary>
    public class ObaController : ApiController
    {
        /// <summary>
        /// 创建OBA
        /// </summary>
        [HttpPost]
        public ResultJson<CreateObaResponse> CreateOba([FromBody] CreateObaRequest request)
        {
            new ObaMgr().CreateOba(new VObaInspect
            {
                CreateBy = BaseEnv.User.PkId,
                Packs = request.PackNos.Select(_ => new VObaPack { PackNo = _ }).ToList(),
                Sites = request.SiteTypes.Select(_ => new VObaSite { SiteType = _ }).ToList()
            });
            return new ResultJson<CreateObaResponse>();
        }

        /// <summary>
        /// 查询OBA站点
        /// </summary>
        [HttpPost]
        public ResultJson<QueryObaSitesResponse> QueryObaSites([FromBody] QueryObaSitesRequest request)
        {
            return new ResultJson<QueryObaSitesResponse>(new QueryObaSitesResponse
            {
                Sites = new List<ObaSite>
                {
                    new ObaSite { SiteType = DataAccess.Enum.SiteType.NQC },
                    new ObaSite { SiteType = DataAccess.Enum.SiteType.EL2 },
                    new ObaSite { SiteType = DataAccess.Enum.SiteType.IVTest },
                    new ObaSite { SiteType = DataAccess.Enum.SiteType.InsulatedResistance }
                }
            });
        }

        /// <summary>
        /// 完结OBA
        /// </summary>
        [HttpPost]
        public ResultJson<FinishObaResponse> FinishOba([FromBody] FinishObaRequest request)
        {
            new ObaMgr().FinishOba(new VObaInspect
            {
                ObaNo = request.ObaNo,
                ModifyBy = BaseEnv.User.PkId
            });
            return new ResultJson<FinishObaResponse>();
        }

        /// <summary>
        /// 查询OBA列表
        /// </summary>
        [HttpPost]
        public ResultJson<QueryObasResponse> QueryObas([FromBody] QueryObasRequest request)
        {
            var total = 0;
            var models = new ObaMgr().QueryObas(request.PageIndex, request.PageSize, out total, string.Empty, string.Empty, request.Status, null, null);
            return new ResultJson<QueryObasResponse>(new QueryObasResponse
            {
                Obas = models.Select(model => new Oba
                {
                    ObaNo = model.ObaNo,
                    OrderNo = model.OrderNo,
                    Status = model.Status,
                    CreateTime = model.CreateTime,
                    FinishTime = model.FinishTime,
                    CNCreateBy = model.CNCreateBy,
                    Packs = model.Packs.Select(_ => new ObaPack
                    {
                        PackBatchNo = _.PackBatchNo,
                        PackNo = _.PackNo
                    }).ToList(),
                    Sites = model.Sites.Select(_ => new ObaSite
                    {
                        SiteType = _.SiteType
                    }).ToList()
                }).ToList()
            });
        }

        /// <summary>
        /// 获取OBA对象
        /// </summary>
        [HttpPost]
        public ResultJson<GetObaResponse> GetOba([FromBody] GetObaRequest request)
        {
            var model = new ObaMgr().GetOba(request.ObaNo);
            return new ResultJson<GetObaResponse>(new GetObaResponse
            {
                Oba = model == null ? null : new Oba
                {
                    OrderNo = model.OrderNo,
                    ObaNo = model.ObaNo,
                    CNCreateBy = model.CNCreateBy,
                    CreateTime = model.CreateTime,
                    FinishTime = model.FinishTime,
                    Status = model.Status,
                    Packs = model.Packs.Select(_ => new ObaPack
                    {
                        PackBatchNo = _.PackBatchNo,
                        PackNo = _.PackNo
                    }).ToList(),
                    Sites = model.Sites.Select(_ => new ObaSite
                    {
                        SiteType = _.SiteType
                    }).ToList()
                }
            });
        }
    }
}
